home *** CD-ROM | disk | FTP | other *** search
/ Aminet 20 / Aminet 20 (1997)(GTI - Schatztruhe)[!][Aug 1997].iso / Aminet / comm / www / HTP.lha / HTP / source / option.c < prev    next >
C/C++ Source or Header  |  1997-06-21  |  10KB  |  404 lines

  1. /*
  2. //
  3. // option.c
  4. //
  5. // Program options and setting
  6. //
  7. // Copyright (c) 1995-96 Jim Nelson.  Permission to distribute
  8. // granted by the author.  No warranties are made on the fitness of this
  9. // source code.
  10. // Amiga version - 1997 - Geert Bevin
  11. //
  12. */
  13.  
  14. #include "htp.h"
  15.  
  16. static VARSTORE globalOption;
  17. static VARSTORE localOption;
  18. static VARSTORE *currentContext = NULL;
  19.  
  20. /* option variable types */
  21. #define VAR_TYPE_OPTION                 (0)
  22.  
  23. /* option strings (user, not internal names) */
  24. const char *OPT_STR_IMGXY = "IMGXY";
  25. const char *OPT_STR_NOIMGXY = "NOIMGXY";
  26. const char *OPT_STR_VERBOSE = "VERBOSE";
  27. const char *OPT_STR_QUIET = "QUIET";
  28. const char *OPT_STR_DEPEND = "DEPEND";
  29. const char *OPT_STR_NODEPEND = "NODEPEND";
  30. const char *OPT_STR_PRECIOUS = "PRECIOUS";
  31. const char *OPT_STR_NOPRECIOUS = "NOPRECIOUS";
  32. const char *OPT_STR_CONDENSE = "CONDENSE";
  33. const char *OPT_STR_NOCONDENSE = "NOCONDENSE";
  34. const char *OPT_STR_SET_DELIM = "DELIM";
  35. const char *OPT_STR_DELIM_HTML = "HTML";
  36. const char *OPT_STR_DELIM_SQUARE = "SQUARE";
  37. const char *OPT_STR_DELIM_CURLY = "CURLY";
  38. const char *OPT_STR_KEEP_TEMP = "KEEPTEMP";
  39.  
  40. /* option string names */
  41. const char *OPT_N_IMGXY = "IMGXY";
  42. const char *OPT_N_QUIET = "QUIET";
  43. const char *OPT_N_DEPEND = "DEPEND";
  44. const char *OPT_N_PRECIOUS = "PRECIOUS";
  45. const char *OPT_N_CONDENSE = "CONDENSE";
  46. const char *OPT_N_USAGE = "USAGE";
  47. const char *OPT_N_SET_DELIM = "DELIM";
  48. const char *OPT_N_KEEP_TEMP = "KEEPTEMP";
  49.  
  50. /* option string values */
  51. const char *OPT_V_TRUE = "TRUE";
  52. const char *OPT_V_FALSE = "FALSE";
  53. const char *OPT_V_DELIM_HTML = "HTML";
  54. const char *OPT_V_DELIM_SQUARE = "SQUARE";
  55. const char *OPT_V_DELIM_CURLY = "CURLY";
  56.  
  57. /*
  58. //
  59. */
  60.  
  61. BOOL SetOption(const char *name, const char *value)
  62. {
  63.     return StoreVariable(currentContext, name, value, VAR_TYPE_OPTION,
  64.         0, NULL, NULL);
  65. }
  66.  
  67. BOOL InitOptionHelper(const char *name, const char *value, OPTION_CALLBACK optionCallback,
  68.     ULONG userParam)
  69. {
  70.     BOOL store;
  71.  
  72.     store = TRUE;
  73.     if(optionCallback != NULL)
  74.     {
  75.         store = optionCallback(name, value, userParam);
  76.     }
  77.  
  78.     if(store)
  79.     {
  80.         if(SetOption(name, value) == FALSE)
  81.         {
  82.             return FALSE;
  83.         }
  84.     }
  85.  
  86.     return TRUE;
  87. }
  88.  
  89. BOOL InitializeGlobalOption(OPTION_CALLBACK optionCallback, ULONG userParam)
  90. {
  91.     BOOL result;
  92.  
  93.     if(InitializeVariableStore(&globalOption) == FALSE)
  94.     {
  95.         return FALSE;
  96.     }
  97.  
  98.     currentContext = &globalOption;
  99.  
  100.     /* set defaults for all options */
  101.     result = InitOptionHelper(OPT_N_IMGXY, OPT_V_TRUE, optionCallback, userParam);
  102.     result = InitOptionHelper(OPT_N_QUIET, OPT_V_FALSE, optionCallback, userParam) && result;
  103.     result = InitOptionHelper(OPT_N_DEPEND, OPT_V_TRUE, optionCallback, userParam) && result;
  104.     result = InitOptionHelper(OPT_N_PRECIOUS, OPT_V_FALSE, optionCallback, userParam) && result;
  105.     result = InitOptionHelper(OPT_N_CONDENSE, OPT_V_FALSE, optionCallback, userParam) && result;
  106.     result = InitOptionHelper(OPT_N_USAGE, OPT_V_FALSE, optionCallback, userParam) && result;
  107.     result = InitOptionHelper(OPT_N_SET_DELIM, OPT_V_DELIM_HTML, optionCallback, userParam) && result;
  108.     result = InitOptionHelper(OPT_N_KEEP_TEMP, OPT_V_FALSE, optionCallback, userParam) && result;
  109.  
  110.     /* if a problem occurred, back out */
  111.     if(result == FALSE)
  112.     {
  113.         DEBUG_PRINT(("Unable to store global defaults\n"));
  114.         DestroyGlobalOption();
  115.     }
  116.  
  117.     return result;
  118. }
  119.  
  120. void DestroyGlobalOption(void)
  121. {
  122.     currentContext = NULL;
  123.     DestroyVariableStore(&globalOption);
  124. }
  125.  
  126. BOOL InitializeLocalOption(void)
  127. {
  128.     if(InitializeVariableStore(&localOption) == TRUE)
  129.     {
  130.         PushVariableStoreContext(&globalOption, &localOption);
  131.         currentContext = &localOption;
  132.         return TRUE;
  133.     }
  134.  
  135.     return FALSE;
  136. }
  137.  
  138. void DestroyLocalOption(void)
  139. {
  140.     currentContext = &globalOption;
  141.     DestroyVariableStore(&localOption);
  142. }
  143.  
  144. BOOL ParseToken(const char *token, OPTION_CALLBACK optionCallback, ULONG userParam)
  145. {
  146.     const char *name;
  147.     const char *value;
  148.     BOOL store;
  149.     char *tokenCopy;
  150.     char *tokenPtr;
  151.     char *tokenValue;
  152.     FIND_TOKEN findToken;
  153.  
  154.     name = NULL;
  155.     value = NULL;
  156.  
  157.     /* process the string ... probably a better way to do this */
  158.     /* rather than brute force, but for now, it works */
  159.     if(stricmp(token, OPT_STR_IMGXY) == 0)
  160.     {
  161.         name = OPT_N_IMGXY;
  162.         value = OPT_V_TRUE;
  163.     }
  164.     else if(stricmp(token, OPT_STR_NOIMGXY) == 0)
  165.     {
  166.         name = OPT_N_IMGXY;
  167.         value = OPT_V_FALSE;
  168.     }
  169.     else if(stricmp(token, OPT_STR_VERBOSE) == 0)
  170.     {
  171.         name = OPT_N_QUIET;
  172.         value = OPT_V_FALSE;
  173.     }
  174.     else if(stricmp(token, OPT_STR_QUIET) == 0)
  175.     {
  176.         name = OPT_N_QUIET;
  177.         value = OPT_V_TRUE;
  178.     }
  179.     else if(stricmp(token, OPT_STR_DEPEND) == 0)
  180.     {
  181.         name = OPT_N_DEPEND;
  182.         value = OPT_V_TRUE;
  183.     }
  184.     else if(stricmp(token, OPT_STR_NODEPEND) == 0)
  185.     {
  186.         name = OPT_N_DEPEND;
  187.         value = OPT_V_FALSE;
  188.     }
  189.     else if(stricmp(token, OPT_STR_PRECIOUS) == 0)
  190.     {
  191.         name = OPT_N_PRECIOUS;
  192.         value = OPT_V_TRUE;
  193.     }
  194.     else if(stricmp(token, OPT_STR_NOPRECIOUS) == 0)
  195.     {
  196.         name = OPT_N_PRECIOUS;
  197.         value = OPT_V_FALSE;
  198.     }
  199.     else if(stricmp(token, OPT_STR_CONDENSE) == 0)
  200.     {
  201.         name = OPT_N_CONDENSE;
  202.         value = OPT_V_TRUE;
  203.     }
  204.     else if(stricmp(token, OPT_STR_NOCONDENSE) == 0)
  205.     {
  206.         name = OPT_N_CONDENSE;
  207.         value = OPT_V_FALSE;
  208.     }
  209.     else if(stricmp(token, OPT_STR_KEEP_TEMP) == 0)
  210.     {
  211.         name = OPT_N_KEEP_TEMP;
  212.         value = OPT_V_TRUE;
  213.     }
  214.     else if((stricmp(token, "?") == 0) || (stricmp(token, "H") == 0))
  215.     {
  216.         name = OPT_N_USAGE;
  217.         value = OPT_V_TRUE;
  218.     }
  219.     else
  220.     {
  221.         tokenPtr = NULL;
  222.         tokenValue = NULL;
  223.  
  224.         /* further break up the token, looking for an equal sign */
  225.         if((tokenCopy = DuplicateString(token)) == NULL)
  226.         {
  227.             DEBUG_PRINT(("unable to duplicate token string"));
  228.             return FALSE;
  229.         }
  230.  
  231.         tokenPtr = StringFirstToken(&findToken, tokenCopy, "=");
  232.         if(tokenPtr != NULL)
  233.         {
  234.             tokenValue = StringNextToken(&findToken);
  235.         }
  236.  
  237.         if((tokenPtr != NULL) && (tokenValue != NULL))
  238.         {
  239.             /* tokenPtr now points to the left side of the equal sign (if there */
  240.             /* is one */
  241.             if(stricmp(tokenPtr, OPT_STR_SET_DELIM) == 0)
  242.             {
  243.                 name = OPT_N_SET_DELIM;
  244.  
  245.                 if(stricmp(tokenValue, OPT_STR_DELIM_HTML) == 0)
  246.                 {
  247.                     value = OPT_V_DELIM_HTML;
  248.                 }
  249.                 else if(stricmp(tokenValue, OPT_STR_DELIM_SQUARE) == 0)
  250.                 {
  251.                     value = OPT_V_DELIM_SQUARE;
  252.                 }
  253.                 else if(stricmp(tokenValue, OPT_STR_DELIM_CURLY) == 0)
  254.                 {
  255.                     value = OPT_V_DELIM_CURLY;
  256.                 }
  257.                 else
  258.                 {
  259.                     /* something screwy here! */
  260.                     name = NULL;
  261.                     value = NULL;
  262.                 }
  263.             }
  264.         }
  265.  
  266.         FreeMemory(tokenCopy);
  267.         tokenCopy = NULL;
  268.     }
  269.  
  270.     if(name != NULL)
  271.     {
  272.         assert(value != NULL);
  273.  
  274.         store = TRUE;
  275.         if(optionCallback != NULL)
  276.         {
  277.             store = optionCallback(name, value, userParam);
  278.         }
  279.  
  280.         if(store)
  281.         {
  282.             return SetOption(name, value);
  283.         }
  284.  
  285.         return TRUE;
  286.     }
  287.     else if(optionCallback != NULL)
  288.     {
  289.         /* report the bad string through the option callback */
  290.         return optionCallback(NULL, token, userParam);
  291.     }
  292.  
  293.     /* FALSE indicates an error only */
  294.     return FALSE;
  295. }
  296.  
  297. BOOL ParseTextOption(const char *string, OPTION_CALLBACK optionCallback,
  298.     ULONG userParam)
  299. {
  300.     char *tokenize;
  301.     char *ptr;
  302.     BOOL result;
  303.     FIND_TOKEN findToken;
  304.  
  305.     /* copy the string to run through String...Token() */
  306.     if((tokenize = DuplicateString(string)) == NULL)
  307.     {
  308.         DEBUG_PRINT(("unable to allocate duplicate string"));
  309.         return FALSE;
  310.     }
  311.  
  312.     result = TRUE;
  313.     ptr = StringFirstToken(&findToken, tokenize, " ");
  314.     while(ptr != NULL)
  315.     {
  316.         /* skip any leading slashes or dashes */
  317.         while((((*ptr == '/') || (*ptr == '-'))) && (*ptr != NUL))
  318.         {
  319.             ptr++;
  320.         }
  321.  
  322.         /* parse this token */
  323.         if(ParseToken(ptr, optionCallback, userParam) == FALSE)
  324.         {
  325.             result = FALSE;
  326.             break;
  327.         }
  328.  
  329.         /* do it till the cows come home */
  330.         ptr = StringNextToken(&findToken);
  331.     }
  332.  
  333.     FreeMemory(tokenize);
  334.  
  335.     return result;
  336. }
  337.  
  338. BOOL ParseMarkupOption(const HTML_MARKUP *htmlMarkup, OPTION_CALLBACK optionCallback,
  339.     ULONG userParam)
  340. {
  341.     uint ctr;
  342.     char token[128];
  343.     BOOL result;
  344.  
  345.     result = TRUE;
  346.  
  347.     /* technically, would like to avoid iterating through the HTML_MARKUP */
  348.     /* structure for certain (future design) reasons, but really have to */
  349.     /* for this to work */
  350.     for(ctr = 0; ctr < htmlMarkup->attribCount; ctr++)
  351.     {
  352.         /* have to build the token as well ... kind of hurts to break up */
  353.         /* the attribute only to put it back together again */
  354.         StringCopy(token, htmlMarkup->attrib[ctr].name, sizeof(token));
  355.         if(htmlMarkup->attrib[ctr].value != NULL)
  356.         {
  357.             strncat(token, "=", sizeof(token));
  358.             strncat(token, htmlMarkup->attrib[ctr].value, sizeof(token));
  359.         }
  360.  
  361.         if(ParseToken(token, optionCallback, userParam) == FALSE)
  362.         {
  363.             result = FALSE;
  364.             break;
  365.         }
  366.     }
  367.  
  368.     return result;
  369. }
  370.  
  371. BOOL IsOptionEnabled(const char *name)
  372. {
  373.     const char *value;
  374.  
  375.     value = GetVariableValue(currentContext, name);
  376.  
  377.     /* !! ugh ... probably shouldnt assert() these, but for now ... */
  378.     /* first case means that the caller passed in a bogus option name */
  379.     /* second case means that the option isnt boolean */
  380.     if(value == NULL)
  381.     {
  382.         DEBUG_PRINT(("looking up %s", name));
  383.     }
  384.     assert(value != NULL);
  385.     assert((stricmp(value, OPT_V_TRUE) == 0) || (stricmp(value, OPT_V_FALSE) == 0));
  386.  
  387.     if(stricmp(value, OPT_V_TRUE) == 0)
  388.     {
  389.         return TRUE;
  390.     }
  391.  
  392.     return FALSE;
  393. }
  394.  
  395. const char *GetOptionValue(const char *name)
  396. {
  397.     const char *value;
  398.  
  399.     value = GetVariableValue(currentContext, name);
  400.     assert(value != NULL);
  401.     return value;
  402. }
  403.  
  404.